﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HAMath.InverseDistance;



namespace HA.ModFlow.Domain.DomainLayer
{

    /// <summary>
    /// 利用 反距离平方法 计算网格外围边框的点的高程值
    /// 参考网址：http://wenku.baidu.com/view/67aa256d1eb91a37f1115cec.html
    /// </summary>
    public class GetBoundGridElevation
    {

        /// <summary>
        /// 存放平面所有面单元中心点XY
        /// </summary>
        static List<Point3D> _centerPoint;

        /// <summary>
        /// 存放平面所有面单元中心点Z
        /// </summary>
        static List<double> _planeCenterPointElevation;




        /// <summary>
        /// 取得外围网格的某一层的平面网格的单元的中心点信息
        /// 利用3个参数组合出  面中心点的坐标信息    
        /// 
        /// 平面点的XY存在_centerPoint
        /// 中心点的Z存在_planeCenterPointElevation
        /// 
        /// </summary>  
        /// <param name="layerPointElevation">平面单元中心点高程值</param>
        /// <param name="rowCoords"> row(Y) 方向的外围边框点坐标信息</param>
        /// <param name="colCoords"> col(X) 方向的外围边框点坐标信息</param>
        /// 
        public static bool GetPlaneCellCenterPoint(double[,] layerPointElevation, double[] rowCoords, double[] colCoords)
        //public static bool  GetPlaneCellCenterPoint(List<Point3D> point, List<double> planeCenterEle, double[,] layerPointElevation, double[] rowCoords, double[] colCoords)
        {
            try
            {
                List<Point3D> point = new List<Point3D>();

                //遍历平面单元格

                //for (int i = 0; i < (colCoords.Length - 1) * (rowCoords.Length - 1); i++)
                //{
                //    Point3D point3d = new Point3D();

                //    //平面网格单元中心点的X Y坐标  规则的矩形吗？会平行四边形吗？  暂时按照矩形处理

                //    point3d.X = (colCoords[i % (colCoords.Length - 1)] + colCoords[i % (colCoords.Length - 1) + 1]) / 2;//X坐标
                //    point3d.Y = (rowCoords[i % (rowCoords.Length - 1)] + rowCoords[i % (rowCoords.Length - 1) + 1]) / 2;//Y坐标

                //    point.Add(point3d);

                //}

                //从左上角为第一个点开始 X由小变大 Y由大变小
                for (int row = 0; row < rowCoords.Length - 1; row++)
                {
                    for (int cols = 0; cols < colCoords.Length - 1; cols++)
                    {

                        Point3D point3d = new Point3D();

                        //平面网格单元中心点的X Y坐标  规则的矩形吗？会平行四边形吗？  暂时按照矩形处理

                        point3d.X = (colCoords[cols] + colCoords[cols + 1]) / 2;//X坐标

                        //point3d.Y = (rowCoords[row] + rowCoords[row + 1]) / 2;//Y坐标
                        point3d.Y = (rowCoords[rowCoords.Length - 2 - row] + rowCoords[rowCoords.Length - 1 - row]) / 2;//Y坐标


                        point.Add(point3d);


                    }
                }





                List<double> planeCenterEle = new List<double>();

                //平面网格单元中心点的 Z 坐标 

                for (int i = 0; i < layerPointElevation.GetLength(0); i++)//row Y
                {
                    for (int j = 0; j < layerPointElevation.GetLength(1); j++)//col  X
                    {
                        planeCenterEle.Add(layerPointElevation[i, j]);

                    }
                }

                //for (int i = layerPointElevation.GetLength(0)-1; i >-1 ; i--)//row Y
                //{
                //    for (int j = 0; j < layerPointElevation.GetLength(1); j++)//col  X
                //    {
                //        planeCenterEle.Add(layerPointElevation[i, j]);

                //    }
                //}


                if (_centerPoint != null)
                {
                    _centerPoint.Clear();
                }

                _centerPoint = point;

                if (_planeCenterPointElevation != null)
                {
                    _planeCenterPointElevation.Clear();
                }

                _planeCenterPointElevation = planeCenterEle;

                return true;
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("取得外围网格的平面网格的单元的中心点信息时出错！");
                return false;
            }


        }





        //static List<Point3D> _samplePoint;//存储采样点XY
        //static List<double> _samplePointElevation ;//存采样点高程

        /// <summary>
        /// 取得观测点（位于边框上的点）的高程值
        /// </summary>
        /// <param name="point3d">观测点信息</param>
        /// <param name="rows">观测点在数组中的rows</param>
        /// <param name="cols">观测点在数组中的cols</param>
        /// <param name="rowsNum">外围边框平面rows总数</param>
        /// <param name="colsNum">外围边框平面cols总数</param>
        /// <returns></returns>
        public static double GetOutlinerPoint(HA.ModFlow.Domain.DomainLayer.Point3D point3d, int rows, int cols, int rowsNum, int colsNum)
        {
            //将点信息由（HA .ModFlow .Domain .DomainLayer .Point3D）表示成 Ha。Math 类库能够识别的表示（ HAMath.InverseDistance.Point3D）
            HAMath.InverseDistance.Point3D haMathPoint3D = new HAMath.InverseDistance.Point3D();
            haMathPoint3D.X = point3d.X;
            haMathPoint3D.Y = point3d.Y;
            haMathPoint3D.Z = point3d.Z;


            List<HAMath.InverseDistance.Point3D> _samplePoint = new List<HAMath.InverseDistance.Point3D>();//存储采样点XY
            List<double> _samplePointElevation = new List<double>();//存采样点高程



            #region

            //List<Point3D> _samplePoint = new List<Point3D>();//存储采样点XY
            //List<double> _samplePointElevation = new List<double>();//存采样点高程

            _samplePoint.Clear();
            _samplePointElevation.Clear();


            #region 四个角点  周围有1个点

            if (rows == 0 && cols == 0)
            {
                return _planeCenterPointElevation[0];
            }

            if (rows == 0 && cols == colsNum - 1)
            {
                return _planeCenterPointElevation[colsNum - 2];
            }

            if (rows == rowsNum - 1 && cols == 0)
            {
                return _planeCenterPointElevation[(rowsNum - 2) * (colsNum - 1)];
            }

            if (rows == rowsNum - 1 && cols == colsNum - 1)
            {
                return _planeCenterPointElevation[rows * cols - 1];
            }

            #endregion  四个角点  周围有1个点  结束


            #region  边框上的点(不包括角点) 周围有2个点

            if (rows == 0 && cols != colsNum - 1 && cols != 0)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();

                for (int i = 0; i < 2; i++)
                {
                    //Point3D point = new Point3D();
                    HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                    point.X = _centerPoint[cols - i].X;
                    point.Y = _centerPoint[cols - i].Y;
                    point.Z = _centerPoint[cols - i].Z;

                    _samplePoint.Add(point);

                }
                for (int i = 0; i < 2; i++)
                {
                    _samplePointElevation.Add(_planeCenterPointElevation[cols - i]);
                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);

            }

            if (rows == rowsNum - 1 && cols != colsNum - 1 && cols != 0)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();

                for (int i = 0; i < 2; i++)
                {
                    //Point3D point = new Point3D();
                    HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                    point.X = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].X;
                    point.Y = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].Y;
                    point.Z = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].Z;

                    _samplePoint.Add(point);

                }
                for (int i = 0; i < 2; i++)
                {
                    _samplePointElevation.Add(_planeCenterPointElevation[(rows - 1) * (colsNum - 1) + cols - i]);
                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);


            }

            if (cols == 0 && rows != rowsNum - 1 && rows != 0)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();

                for (int i = 0; i < 2; i++)
                {
                    //Point3D point = new Point3D();
                    HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                    point.X = _centerPoint[(rows - i) * (colsNum - 1)].X;
                    point.Y = _centerPoint[(rows - i) * (colsNum - 1)].Y;
                    point.Z = _centerPoint[(rows - i) * (colsNum - 1)].Z;

                    _samplePoint.Add(point);

                }
                for (int i = 0; i < 2; i++)
                {
                    _samplePointElevation.Add(_planeCenterPointElevation[(rows - i) * (colsNum - 1)]);
                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);


            }

            if (cols == colsNum - 1 && rows != rowsNum - 1 && rows != 0)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();

                for (int i = 0; i < 2; i++)
                {
                    //Point3D point = new Point3D();
                    HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                    point.X = _centerPoint[(rows - i) * (colsNum - 1) + cols - 1].X;
                    point.Y = _centerPoint[(rows - i) * (colsNum - 1) + cols - 1].Y;
                    point.Z = _centerPoint[(rows - i) * (colsNum - 1) + cols - 1].Z;

                    _samplePoint.Add(point);

                }
                for (int i = 0; i < 2; i++)
                {
                    _samplePointElevation.Add(_planeCenterPointElevation[(rows - i) * (colsNum - 1) + cols - 1]);
                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);


            }

            #endregion 边框上的点(不包括角点) 周围有2个点  结束


            #region 中间的点  周围有4个点

            if (rows != 0 && rows != rowsNum - 1 && cols != 0 && cols != colsNum - 1)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();

                for (int i = 0; i < 2; i++)//下侧2个点
                {
                    //Point3D point = new Point3D();
                    HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                    point.X = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].X;
                    point.Y = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].Y;
                    point.Z = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].Z;

                    _samplePoint.Add(point);

                }
                for (int i = 0; i < 2; i++)//上侧2个点
                {
                    //Point3D point = new Point3D();
                    HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();


                    point.X = _centerPoint[rows * (colsNum - 1) + cols - i].X;
                    point.Y = _centerPoint[rows * (colsNum - 1) + cols - i].Y;
                    point.Z = _centerPoint[rows * (colsNum - 1) + cols - i].Z;

                    _samplePoint.Add(point);

                }

                for (int i = 0; i < 2; i++)//下侧2个点
                {
                    _samplePointElevation.Add(_planeCenterPointElevation[(rows - 1) * (colsNum - 1) + cols - i]);
                }
                for (int i = 0; i < 2; i++)//上侧2个点
                {
                    _samplePointElevation.Add(_planeCenterPointElevation[rows * (colsNum - 1) + cols - i]);
                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);



            }






            #endregion 中间的点  周围有4个点 结束


            #endregion


            //return point3d.Z;
            return haMathPoint3D.Z;

        }





        /// <summary>
        /// 取得观测点（位于边框上的点）的高程值
        /// </summary>
        /// <param name="defaultElevation">点A在网格坐标系下的坐标</param>
        /// <param name="point3d">观测点信息</param>
        /// <param name="rows">边框点Index 从0 开始</param>
        /// <param name="cols">边框点Index 从0 开始</param>
        /// <param name="rowsNum">外围边框平面rows总数</param>
        /// <param name="colsNum">外围边框平面cols总数</param>
        /// <param name="topCellIBound"></param>
        /// <param name="botCellIBound"></param>
        /// <returns></returns>
        public static double GetOutlinerPoint(double defaultElevation, HA.ModFlow.Domain.DomainLayer.Point3D point3d, int rows, int cols, int rowsNum, int colsNum, int[,] topCellIBound, int[,] botCellIBound)
        {
            if (topCellIBound == null || botCellIBound == null)
            {
                if (topCellIBound == null)
                {
                    topCellIBound = new int[rows - 1, cols - 1];
                    for (int rowIndex = 0; rowIndex < rows - 1; rowIndex++)
                    {
                        for (int colIndex = 0; colIndex < cols - 1; colIndex++)
                        {
                            topCellIBound[rowIndex, colIndex] = 1;
                        }
                    }

                }

                if (botCellIBound == null)
                {
                    botCellIBound = new int[rows - 1, cols - 1];
                    for (int rowIndex = 0; rowIndex < rows - 1; rowIndex++)
                    {
                        for (int colIndex = 0; colIndex < cols - 1; colIndex++)
                        {
                            botCellIBound[rowIndex, colIndex] = 1;
                        }
                    }

                }

            }

            //将点信息由（HA .ModFlow .Domain .DomainLayer .Point3D）表示成 Ha。Math 类库能够识别的表示（ HAMath.InverseDistance.Point3D）
            HAMath.InverseDistance.Point3D haMathPoint3D = new HAMath.InverseDistance.Point3D();
            haMathPoint3D.X = point3d.X;
            haMathPoint3D.Y = point3d.Y;
            haMathPoint3D.Z = point3d.Z;


            List<HAMath.InverseDistance.Point3D> _samplePoint = new List<HAMath.InverseDistance.Point3D>();//存储采样点XY
            List<double> _samplePointElevation = new List<double>();//存采样点高程



            #region

            //List<Point3D> _samplePoint = new List<Point3D>();//存储采样点XY
            //List<double> _samplePointElevation = new List<double>();//存采样点高程

            _samplePoint.Clear();
            _samplePointElevation.Clear();


            #region 四个角点  周围有1个点

            if (rows == 0 && cols == 0)
            {
                if (topCellIBound[0, 0] == 0 && botCellIBound[0, 0] == 0)
                {
                    //所有周边相邻单元格都无效
                    return defaultElevation;

                }
                else
                {
                    //上部或者下部单元格至少有一个有效

                    return _planeCenterPointElevation[0];
                }

            }

            if (rows == 0 && cols == colsNum - 1)
            {


                if (topCellIBound[0, colsNum - 2] == 0 && botCellIBound[0, colsNum - 2] == 0)
                {
                    //所有周边相邻单元格都无效
                    return defaultElevation;
                }
                else
                {
                    //上部或者下部单元格至少有一个有效
                    return _planeCenterPointElevation[colsNum - 2];
                }





            }

            if (rows == rowsNum - 1 && cols == 0)
            {
                if (topCellIBound[rowsNum - 2, 0] == 0 && botCellIBound[rowsNum - 2, 0] == 0)
                {
                    //所有周边相邻单元格都无效
                    return defaultElevation;

                }
                else
                {
                    //上部或者下部单元格至少有一个有效
                    return _planeCenterPointElevation[(rowsNum - 2) * (colsNum - 1)];
                }

            }

            if (rows == rowsNum - 1 && cols == colsNum - 1)
            {
                if (topCellIBound[rowsNum - 2, colsNum - 2] == 0 && botCellIBound[rowsNum - 2, colsNum - 2] == 0)
                {
                    //所有周边相邻单元格都无效
                    return defaultElevation;
                }
                else
                {
                    //上部或者下部单元格至少有一个有效
                    return _planeCenterPointElevation[rows * cols - 1];
                }

            }

            #endregion  四个角点  周围有1个点  结束


            #region  边框上的点(不包括角点) 周围有2个点

            if (rows == 0 && cols != colsNum - 1 && cols != 0)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();




                for (int i = 0; i < 2; i++)
                {
                    //Point3D point = new Point3D();

                    if (topCellIBound[rows, cols - i] == 0 && botCellIBound[rows, cols - i] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                        point.X = _centerPoint[cols - i].X;
                        point.Y = _centerPoint[cols - i].Y;
                        point.Z = _centerPoint[cols - i].Z;

                        _samplePoint.Add(point);

                    }


                }

                for (int i = 0; i < 2; i++)
                {

                    if (topCellIBound[rows, cols - i] == 0 && botCellIBound[rows, cols - i] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        _samplePointElevation.Add(_planeCenterPointElevation[cols - i]);
                    }

                }


                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);

            }

            if (rows == rowsNum - 1 && cols != colsNum - 1 && cols != 0)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();






                for (int i = 0; i < 2; i++)
                {
                    //Point3D point = new Point3D();


                    if (topCellIBound[rows - 1, cols - i] == 0 && botCellIBound[rows - 1, cols - i] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                        point.X = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].X;
                        point.Y = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].Y;
                        point.Z = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].Z;

                        _samplePoint.Add(point);

                    }

                }




                for (int i = 0; i < 2; i++)
                {
                    if (topCellIBound[rows - 1, cols - i] == 0 && botCellIBound[rows - 1, cols - i] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {

                        //上部或者下部单元格至少有一个有效

                        _samplePointElevation.Add(_planeCenterPointElevation[(rows - 1) * (colsNum - 1) + cols - i]);

                    }

                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);


            }

            if (cols == 0 && rows != rowsNum - 1 && rows != 0)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();

                for (int i = 0; i < 2; i++)
                {
                    //Point3D point = new Point3D();

                    if (topCellIBound[rows - i, 0] == 0 && botCellIBound[rows - i, 0] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                        point.X = _centerPoint[(rows - i) * (colsNum - 1)].X;
                        point.Y = _centerPoint[(rows - i) * (colsNum - 1)].Y;
                        point.Z = _centerPoint[(rows - i) * (colsNum - 1)].Z;

                        _samplePoint.Add(point);


                    }

                }
                for (int i = 0; i < 2; i++)
                {
                    if (topCellIBound[rows - i, 0] == 0 && botCellIBound[rows - i, 0] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        _samplePointElevation.Add(_planeCenterPointElevation[(rows - i) * (colsNum - 1)]);

                    }


                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);


            }

            if (cols == colsNum - 1 && rows != rowsNum - 1 && rows != 0)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();

                for (int i = 0; i < 2; i++)
                {
                    //Point3D point = new Point3D();

                    if (topCellIBound[rows - i, cols - 1] == 0 && botCellIBound[rows - i, cols - 1] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                        point.X = _centerPoint[(rows - i) * (colsNum - 1) + cols - 1].X;
                        point.Y = _centerPoint[(rows - i) * (colsNum - 1) + cols - 1].Y;
                        point.Z = _centerPoint[(rows - i) * (colsNum - 1) + cols - 1].Z;

                        _samplePoint.Add(point);
                    }


                }
                for (int i = 0; i < 2; i++)
                {
                    if (topCellIBound[rows - i, cols - 1] == 0 && botCellIBound[rows - i, cols - 1] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        _samplePointElevation.Add(_planeCenterPointElevation[(rows - i) * (colsNum - 1) + cols - 1]);

                    }
                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);


            }

            #endregion 边框上的点(不包括角点) 周围有2个点  结束


            #region 中间的点  周围有4个点

            if (rows != 0 && rows != rowsNum - 1 && cols != 0 && cols != colsNum - 1)
            {
                _samplePoint.Clear();
                _samplePointElevation.Clear();

                for (int i = 0; i < 2; i++)//下侧2个点
                {
                    //Point3D point = new Point3D();

                    if (topCellIBound[rows, cols - i] == 0 && botCellIBound[rows, cols - i] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();

                        point.X = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].X;
                        point.Y = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].Y;
                        point.Z = _centerPoint[(rows - 1) * (colsNum - 1) + cols - i].Z;

                        _samplePoint.Add(point);

                    }


                }
                for (int i = 0; i < 2; i++)//上侧2个点
                {
                    //Point3D point = new Point3D();

                    if (topCellIBound[rows - 1, cols - i] == 0 && botCellIBound[rows - 1, cols - i] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        HAMath.InverseDistance.Point3D point = new HAMath.InverseDistance.Point3D();


                        point.X = _centerPoint[rows * (colsNum - 1) + cols - i].X;
                        point.Y = _centerPoint[rows * (colsNum - 1) + cols - i].Y;
                        point.Z = _centerPoint[rows * (colsNum - 1) + cols - i].Z;

                        _samplePoint.Add(point);

                    }


                }

                for (int i = 0; i < 2; i++)//下侧2个点
                {

                    if (topCellIBound[rows-1, cols - i] == 0 && botCellIBound[rows-1, cols - i] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        _samplePointElevation.Add(_planeCenterPointElevation[(rows - 1) * (colsNum - 1) + cols - i]);

                    }

                }
                for (int i = 0; i < 2; i++)//上侧2个点
                {

                    if (topCellIBound[rows , cols - i] == 0 && botCellIBound[rows , cols - i] == 0)
                    {
                        //单元格无效
                        //不参与插值
                        //所有周边相邻单元格都无效
                    }
                    else
                    {
                        //上部或者下部单元格至少有一个有效

                        _samplePointElevation.Add(_planeCenterPointElevation[rows * (colsNum - 1) + cols - i]);

                    }

                }

                //return GetPointElevation(point3d, _samplePoint, _samplePointElevation);
                return HAMath.InverseDistance.InverseDistanceWeighted.GetPointElevation(haMathPoint3D, _samplePoint, _samplePointElevation);



            }






            #endregion 中间的点  周围有4个点 结束


            #endregion


            //return point3d.Z;
            return haMathPoint3D.Z;







        }





        #region 反距离平方法   已经转移到类库  HaMath  中

        ///// <summary>
        ///// 取得观测点的高程值
        ///// </summary>
        ///// <param name="point3d">观测点信息</param>
        ///// <param name="_centerPoint">采样点的集合</param>
        ///// <param name="_planeCenterPointElevation">采样点的高程值</param>
        ///// <returns></returns>
        ////public static double GetPointElevation(Point3D point3d)
        //public static double GetPointElevation(Point3D point3d, List<Point3D> _centerPoint, List<double> _planeCenterPointElevation)
        //{

        //    double n = 2.0;
        //    if (_centerPoint.Count == 0)
        //    {
        //        return 0.0;
        //    }
        //    double D = 0.0;//距离的乘方倒数之和
        //    double DB = 0.0;
        //    for (int i = 0; i < _centerPoint.Count; i++)
        //    {
        //        double Distance = GetDistance(point3d, _centerPoint[i]);
        //        if (Distance <= 0.0)
        //        {
        //            D = 0.0;
        //            return _planeCenterPointElevation[i];
        //            //break;
        //        }
        //        else
        //        {
        //            //double Di = 1 / Distance ^ n;
        //            double Di = 1 / (Distance * Distance);///
        //            D += Di;

        //        }
        //    }

        //    for (int i = 0; i < _centerPoint.Count; i++)
        //    {
        //        double Distance = GetDistance(point3d, _centerPoint[i]);
        //        if (Distance <= 0.0)
        //        {
        //            D = 0.0;
        //            return _planeCenterPointElevation[i];
        //        }
        //        else
        //        {
        //            //double Di = 1 / Distance ^ n;
        //            double Di = 1 / (Distance * Distance);/////////////////////////////
        //            DB += _planeCenterPointElevation[i] * Di / D;
        //        }
        //    }

        //    return DB;



        //}
        ///// <summary>
        ///// 得到两个点的平面距离
        ///// </summary>
        ///// <param name="loc1"></param>
        ///// <param name="loc2"></param>
        ///// <returns></returns>
        //private static double GetDistance(Point3D loc1, Point3D loc2)
        //{
        //    double _distance, d1, d2, d3;
        //    d1 = loc1.X - loc2.X;
        //    d2 = loc1.Y - loc2.Y;
        //    //d3 = loc1.Z - loc2.Z;

        //    _distance = Math.Sqrt(d1 * d1 + d2 * d2);
        //    //_distance = Math.Sqrt(d1 * d1 + d2 * d2 + d3 * d3);

        //    return _distance;
        //}



        #endregion 反距离平方法结束











    }


}
